<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="会员名称：" prop="membershipName">
        <el-input placeholder="会员名称" v-model="form.membershipName" class="col" maxlength="10"/>
      </el-form-item>
      <el-form-item label="等级级别：" prop="membershipLevel">
        <el-input-number
            v-model.number="form.membershipLevel"
            placeholder="请输入等级级别"
            :min="0"
            :max="99"
            step-strictly
        ></el-input-number>
      </el-form-item>
      <el-form-item label="累积充值金额：" prop="membershipPrice">
        <el-input-number
            v-model.number="form.membershipPrice"
            placeholder="请输入累积充值金额"
            :min="0"
            :max="999999"
            step-strictly
        ></el-input-number>
      </el-form-item>
      <el-form-item label="享受折扣：" prop="discountRate">
        <el-input-number
            v-model.number="form.discountRate"
            placeholder="请输入享受折扣"
            :min="0"
            :max="1"
            :step="0.01"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="会员权益：" prop="privilegeIds">
        <el-select v-model="form.privilegeIds" placeholder="请选择" class="col" multiple filterable @change="change" style="width:calc(100% - 6px)">
          <el-option
              v-for="item in rightsList"
              :key="item.id"
              :label="item.privilegeName"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:member:grade:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {gradeDetail, saveGrade} from "@/api/member";
import {getForm} from "@/utils/utils";

export default {
  components: {},
  props: {
    id: {
      default: null
    },
    rightsList: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      form: {
        id: null,
        membershipName: '',
        membershipLevel: '',
        membershipPrice: '',
        discountRate: '',
        privilegeIds: []
      },
      rules: {
        membershipName: [{required: true, message: '请填写会员名称', trigger: ['blur']}],
        privilegeIds: [{required: true, message: '请选择会员权益', trigger: ['blur']}],
        membershipLevel: [{required: true, message: '请填写会员等级', trigger: ['blur']}, {
          type: 'number',
          message: '等级必须为数字值'
        }],
        membershipPrice: [{required: true, message: '请填写累积充值金额', trigger: ['blur']}, {
          type: 'number',
          message: '累积充值金额必须为数字值'
        },],
        discountRate: [{required: true, message: '请填写折扣比例', trigger: ['blur']}, {
          type: 'number',
          message: '折扣必须为数字值'
        },],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
      uploads: {
        show: false,
        multiple: true,
      },

    }
  },
  mounted() {
    if (this.id) {
      this.getDetail();
    }
  },
  methods: {
    change(e) {
      this.form.privilegeIds = e
    },
    getDetail() {
      this.formLoading = true
      gradeDetail(this.id).then(res => {
        getForm(res, this.form);
      }).finally(() => {
        this.formLoading = false;
      })
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        saveGrade(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
